<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="ptypeing-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>


		<!--列表-->
		<el-table :data="type" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="sn" label="产品名" width="120" sortable>
			</el-table-column>
			<el-table-column prop="name" label="分类" width="120" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<!--删除@click="后是请求-->
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>



		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :current-page="currentPage"
						   :page-size="pageSize"
						   :total="total"
						   style="float:right;">
			</el-pagination>
		</el-col>





		<!--新增界面
			:visible.sync= 控制模态框是否显示 为true就显示
			:rules  绑定模型层的属性校验规则
			prop= 用来申明校验规则的属性名
			ref= 给当前表单取一个别名 方便后面表单提交的时候，做整体校验
		-->
		<el-dialog :title="title" :visible.sync="typeFormVisible" :close-on-click-modal="false">
			<el-form :model="typeForm" label-width="80px" :rules="typeFormRules" ref="typeForm">
				<el-form-item label="编号" prop="sn">
					<el-input v-model="typeForm.sn" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="分类" prop="name">
					<el-input v-model="typeForm.name" auto-complete="off"></el-input>
				</el-form-item>


			</el-form>


			<div slot="footer" class="dialog-footer">
				<el-button @click.native="typeFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="typeSubmit" :loading="typeLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>




<!--模板区-->

<script>



    export default {
        data() {
            return {
                title:"",
                filters: {
                    keyword: ''
                },
                type: [],
				total: 0,
                currentPage:1,
                pageSize: 10,
                listLoading: false,
                sels: [],//列表选中行

                typeFormVisible: false,//新增界面是否显示
                typeLoading: false,
                typeFormRules: {
                    sn: [
                        { required: true, message: '请输入产品名', trigger: 'blur' }
                    ],
                    name: [
                        { required: true, message: '请输入分类', trigger: 'blur' }
                    ]
                },
                //新增界面数据
                typeForm: {
                    id:null,
                    sn: '',
                    name: ""
                }

            }
        },
        /*分页和高级查询*/


        methods: {
            //高级查询定位到第一页。
            search(){
                /*定位到第一页	*/
                this.currentPage = 1;
                /*再调用分页查询得效果刷新页面*/
                this.getType();
			},
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getType();
            },
            //获取部门列表
            getType() {
                let para = {//请求参数
                    currentPage: this.currentPage,
                    pageSize: this.pageSize,
                    keyword: this.filters.keyword
                };
                this.listLoading = true;//开启忙等框
                //这里发送axios请求从后端拿数据
                this.$http.post("/type",para)
                    .then(result=>{
                        this.listLoading = false;//关闭忙等框
                        result = result.data;//PageList
                        this.type = result.rows;
                        this.total = result.total;

                    })
                    .catch(result=>{
                        this.$message({
                            message: '操作失败！',
                            type: 'error'
                        });
                    })
            },




            /*<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>*/

            //删除
            //传递两个参数到后端。
            handleDel:function (index,row) {
                //固定写法，就是这样写点击删除会弹出这个。
                this.$confirm('确定删除吗?','温馨提示',{
                    //修饰，不用管。
                    type: 'warning'
                    //点击确定按钮
                }).then(()=> {
                    //开启忙等框
                    this.listLoading = true;
                    //发送请求
                    this.$http.delete("/type/"+ row.id)
                    //成功得话
                        .then(result => {
                            //关闭忙等框
                            this.listLoading = false;
                            //获取AjaxResult提示后端
                            result = result.data;
                            //如果成功
                            if (result.success) {
                                this.$confirm({
                                    message: "删除成功",
                                    type: 'success'
                                });
                            } else {
                                this.message({
                                    message: result.message,
                                    type: 'error'
                                });
                            }
                            /*定位到第一页	*/
                            this.currentPage = 1;
                            /*再调用分页查询得效果刷新页面*/
                            this.getType();
                        })
                        .catch(result => {
                            this.message({
                                message: "删除失败",
                                type: 'error'
                            });
                        });
                }).catch(()=>{   //点击取消,就什么都不用管了。
				});
            },



            //显示编辑界面
            handleEdit: function (index, row) {
                //只有一个模态框这里给title赋值为编辑框f
                this.title = "编辑";
                //开启模态框
                this.typeFormVisible = true;
                // Object.assign 克隆
                this.typeForm = Object.assign({}, row);
                this.getDeptTree();//加载部门树
                //拆分dirpath封装成数组 js功底
                if(row.dirPath){
                    let array = row.dirPath.split("/");
                    let parent = [];
                    for (var i=1;i<array.length-1;i++){
                        parent.push(parseInt(array[i]));
                    }
                    this.typeForm.parent = parent;
                }
            },
            //显示新增界面
            handleAdd: function () {
                this.title = "新增";
                this.typeFormVisible = true;//控制新增或者编辑的模态框的显示
                this.typeForm = {//清空历史遗留数据
                    id:null,
                    name: '',
                    sn: "",
                    state: 0,
                    manager: null,
                    parent: null
                };
                this.getDeptTree();//加载部门树
            },
            //新增/编辑
            typeSubmit: function () {
                this.$refs.typeForm.validate((valid) => {
                    if (valid) {//表单校验成功
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.typeLoading = true;//开启提交按钮忙等框
                            let param = Object.assign({}, this.typeForm);
                            //处理后端需要的数据格式
                            if(param.parent){
                                let parent = param.parent;
                                param.parent = {"id":parent[parent.length-1]};
                            }
                            //发送请求
                            this.$http.put("/type",param)
                                .then(result=>{
                                    this.typeLoading = false;//关闭提交按钮忙等框
                                    result = result.data;//AjaxResult
                                    //1.要提示后端业务执行到底是：成功还是失败
                                    if(result.success){
                                        this.typeFormVisible = false;//关闭弹出的新增或修改模态框
                                        this.$message({
                                            message: '操作成功！',
                                            type: 'success'
                                        });
                                    }else{
                                        this.$message({
                                            message: result.message,
                                            type: 'error'
                                        });
                                    }
                                    //定位到第一页
                                    this.currentPage = 1;
                                    //2.刷新页面
                                    this.getType()
                                    //this.$refs['typeForm'].resetFields();
                                })
                                .catch(result=>{
                                    this.typeLoading = false;//关闭提交按钮忙等框
                                    this.$message({
                                        message: '操作失败',
                                        type: 'error'
                                    });
                                });
                        });
                    }
                });
            },
            selsChange: function (sels) {//每点击一次复选框，会把当前页面上所选中的所有行的数据封装到sels
                this.sels = sels;
            },



            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    //开启忙等框。
                    this.listLoading = true;
                    //发送请求，携带数据ids。
                    this.$http.patch("/type",ids)
                        .then(result=>{
                            //3.关闭忙等框
                            this.listLoading = false;//关闭忙等框
                            result = result.data;//AjaxResult
                            //成功还是失败
                            if(result.success){
                                this.$message({
                                    message: '删除成功！',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
                            }
                            //定位到第一页
                            this.currentPage = 1;
                            //调用分页查询方法刷新页面。
                            this.getType();
                        })
                        .catch(result=>{
                            this.$message({
                                message: '操作失败',
                                type: 'error'
                            });
                        });
                }).catch(() => {

                });
            },
        },

		//钩子方法
        mounted() {
            this.getType();
        }
    }

</script>

<style scoped>

</style>
